package mapStruct.mapper;

import mapStruct.dto.UserRoleDTO;
import mapStruct.entity.Role;
import mapStruct.entity.User;
import mapStruct.vo.UserVo;
import mapStruct.vo.UserVo1;
import mapStruct.vo.UserVo2;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserRoleMapper {
    //获取该类自动生成的实现类的实例
    UserRoleMapper INSTANCE = Mappers.getMapper(UserRoleMapper.class);
    //映射
    @Mappings({
            @Mapping(source = "id",target = "userId"),
            @Mapping(source = "username",target = "name"),
            @Mapping(source = "role.roleName",target = "roleName"),
            @Mapping(source = "user.cost",target = "cost")
    })
    UserRoleDTO toUserRoleDTO(User user);

    /**转List*/
    List<UserRoleDTO> toUserRoleDTOList(List<User> users);


    //提供默认方法
    default UserRoleDTO defaultConvert(){
        UserRoleDTO userRoleDTO= new UserRoleDTO();
        userRoleDTO.setUserId(20L);
        userRoleDTO.setName("NONE");
        userRoleDTO.setRoleName("NONE");
        return userRoleDTO;
    }

    /**
     * 直接使用参数作为值
     * @param user
     * @param myRoleName
     * @return
     */
    @Mappings({
            @Mapping(source = "user.id",target = "userId"), //把user中id绑定到目标对象的userId属性中
            @Mapping(source = "user.username",target = "name"),
            @Mapping(source = "myRoleName",target = "roleName")
    })
    UserRoleDTO useParameter(User user,String myRoleName);

    /**
     * 多个参数
     * @param user
     * @param
     */
    //Mappings控制输出属性
    @Mappings({
            @Mapping(source = "user.id",target = "userId"),
            @Mapping(source = "user.username",target = "name"),
            @Mapping(source = "role.roleName",target = "roleName"),
            @Mapping(source = "role.description",target = "description")
    })
    UserRoleDTO toUserRoleDto(User user, Role role);

    /**
     * 更新 Target UserRoleDTO
     * @param user
     * @param userRoleDTO
     */
    @Mappings({
            @Mapping(source ="id",target = "userId"),
            @Mapping(source = "username",target = "name"),
            @Mapping(source = "role.roleName",target = "roleName"),
            @Mapping(source = "role.description",target = "description")
    })
    void update(User user, @MappingTarget UserRoleDTO userRoleDTO);

//    @Mappings({
//            @Mapping(source = "userId",target = "id"),
//            @Mapping(source = "name", target = "username"),
//            @Mapping(source = "roleName", target = "role.roleName"),
//            @Mapping(source = "description",target = "role.description")
//    })
//    void updateDTO(UserRoleDTO userRoleDTO,@MappingTarget User user);


//    @Mappings({
//            @Mapping(source =  "role.name",target = "roleName"),
//            @Mapping(source = "role.description",target = "description"),
//            @Mapping(source = "id",target = "userId")
//    })
    /**map转对象*/
    @Mappings({
            @Mapping(expression = "java(map.get(\"username\"))", target = "username"),
            @Mapping(expression = "java(map.get(\"password\"))", target = "password"),
            @Mapping(expression = "java(map.get(\"phoneNum\"))", target = "phoneNum"),
            @Mapping(expression = "java(map.get(\"email\"))", target = "email"),
            @Mapping(source = "id",target = "id")
    })
    User mapToUser(Map<String,String> map,String id);


    /** date格式转换*/
    @Mappings({
            @Mapping(source = "user.id",target = "userId"),
            @Mapping(source = "user.username",target = "name"),
            @Mapping(source = "role.roleName",target = "roleName"),
            @Mapping(source = "role.description",target = "description"),
            @Mapping(source = "user.birthDate",target = "date",dateFormat = "yyyy-MM-dd")
    })
    UserRoleDTO dtoToUserRoleDto(User user, Role role);


    /** @BeforeMapping   @AfterMapping  全局，无需引用*/
    @AfterMapping
    default void convertNameToUpperCase(@MappingTarget UserRoleDTO userRoleDTO){
        userRoleDTO.setName(userRoleDTO.getName().toUpperCase());
    }
    /**字段属性名完全一致*/
    @Mappings({

    })
    UserVo toUserVo(User user);

    /**字段属性有一个不一样，且进行映射*/
    @Mappings({
            @Mapping(source = "id",target = "userId")
    })
    UserVo1 toUserVo1(User user);

    /**字段属性有一个不一样，且不进行映射*/
    @Mappings({
//            @Mapping(source = "id",target = "userId")
    })
    UserVo1 toUserVo1UnMapping(User user);

    /** 有一个字段类型不一致*，进行映射/*/
    @Mappings({
            @Mapping(source = "cost",target="cost",ignore = true),
            @Mapping(source = "role",target = "role")
    })
    UserVo2 toUserVo2(User user);
}
